const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const ExtractTextWebpackPlugin = require('extract-text-webpack-plugin');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const webpack = require('webpack');
const HappyPack = require('happypack');
const os = require('os');
const happyThreadPool = HappyPack.ThreadPool({ size: os.cpus().length });
const DllReferencePlugin = require('webpack/lib/DllReferencePlugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

//const common = require('./config/webpack.common');

let cssExtRule = {};
if (process.env.NODE_ENV == 'production') {
        cssExtRule = {
            test:/\.css$/,
            use:ExtractTextWebpackPlugin.extract({
                fallback: 'style-loader',
                use:[
                    {
                        loader: 'css-loader',
                        options: {
                            // modules: true, // 为true会将类名打包成hash值
                            importLoaders: 1 // 前面有几个loader 值就是多少
                        }
                    },
                    {
                        loader: 'postcss-loader',
                    }
                ]
            })
        }

} else {
	cssExtRule = {
        test:/\.css$/,
        use:'happypack/loader?id=css',
    }
}

module.exports = {
    devtool:'cheap-source-map',
    target: (process.env.NODE_TARGET == 'react')?'web':'electron-renderer',
    entry: [
        'react-hot-loader/patch',
        './src/index.js'
    ],
    output: {

        filename: 'freechat.[hash:4].js',
        // publicPath: './',
        path: path.resolve(__dirname,'dist')
    },
    module: {
        rules:[
            cssExtRule,
            {
                test: /\.(jpg|jpeg|png|gif)$/,
                use: [
                    {
                        loader: 'url-loader',
                        options: {
                            limit: 1024,
                            name:'static/images/[name].[ext]',
                           // publicPath:'../'
                        }
                    }
                ]
            },
            {
                test: /\.(htm|html)$/,
                use: 'html-withimg-loader'
            },
            {
                test: /\.(eot|ttf|woff|svg)$/,
                use: 'file-loader'
            },
            {
                test:/\.(js|jsx)$/,
                use: 'happypack/loader?id=jsx',
                include: /src/,
                exclude: ['/node_modules/'] 
            }
        ]
    },
    plugins: [
        new CopyWebpackPlugin([{
            from: path.resolve(__dirname,'static/images'),
            to: path.resolve(__dirname,'dist/static/images'),
            toType: 'dir'
        }]),
        new HappyPack({
          id: 'jsx',
          loaders: ['babel-loader'],
          threadPool: happyThreadPool,
          verbose: true,
        }),
        new HappyPack({
            id: 'css',
            loaders: ['style-loader','css-loader','postcss-loader'],
            threadPool: happyThreadPool,
            verbose: true,
        }),
        new DllReferencePlugin({

            manifest: require('./static/config_js/react.manifest.json'),
        }),
        new HtmlWebpackPlugin({
            title: '自由聊天室-FreeChat',
            filename: './index.html',
            favicon: '',
            template: './index.html',
            hash: true,
            inject: 'body'
        }),
        new AddAssetHtmlPlugin({
            filepath: path.resolve(__dirname, './static/config_js/*.dll.js'),
        }),
        new ExtractTextWebpackPlugin('static/css/style.css'),
        require('autoprefixer'),
        new CleanWebpackPlugin('dist'),
        new webpack.HotModuleReplacementPlugin(),
        new webpack.DefinePlugin({
            NODE_ENV:JSON.stringify(process.env.NODE_ENV)
        })
    ],
    devServer: {
        contentBase: path.resolve(__dirname,'dist'),
        host: 'localhost',
        port: 3000,
        open: false,
        hot: true,
        inline: true
    },
    resolve: {
        //root: path.resolve(__dirname,'node_modules'),
        alias: {

        },
        extensions: ['.js','.json','.css','.jsx']
    }
}

